package com.rocky.project.action.model;

import java.util.LinkedList;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.opensymphony.webwork.ServletActionContext;
import com.rocky.project.action.base.BaseAction;
import com.rocky.project.model.admin.Area;
import com.rocky.project.model.family.Family;
import com.rocky.project.model.family.Person;
import com.rocky.project.util.BeanUtil;
import com.rocky.project.util.WebUtil;

/**
 * @author new
 *
 */
public class FamilyAction extends BaseAction {
	private static final long serialVersionUID = -3510474656970657392L;	
	private Family family;
	
	public Family getFamily() {
		return family;
	}
	public void setFamily(Family family) {
		this.family = family;
	}
	@Override
	public String read() throws Exception {
		java.util.List<Object> paramsvalues = new LinkedList<Object>();
		String queryHql = "SELECT m FROM Family AS m WHERE m.deleted = false   ";		
		String keywordFilterValue = WebUtil.getParaValue("keywordFilterValue");
		String id = WebUtil.getParaValue("id");
		if(keywordFilterValue!=null && !keywordFilterValue.equals("") ){
			queryHql +="    and (m.familycode like ? or m.householder.name like ?  ) ";
			paramsvalues.add("%"+keywordFilterValue+"%");
			paramsvalues.add("%"+keywordFilterValue+"%");			
		}	
		if(id!=null && !id.equals("") ){
			queryHql +="    and m.id = ?   ";			
			paramsvalues.add(Long.valueOf(id));			
		}			
		queryHql = queryHql + "    order by m.id desc";
		this.pageRecords = baseService.executeHqlWithParam(queryHql,paramsvalues.toArray(),getStart(), getPageSize());
		this.rowCount = baseService.getCountHqlWithParam(queryHql, paramsvalues.toArray());
		
		this.ajaxResultMap.put("data", this.pageRecords);
		this.ajaxResultMap.put("rowCount", this.rowCount);
		
		if(StringUtils.isNotBlank(this.suc)){
			return this.suc;
		}		
		log.info("FamilyAction:read.action->"+this.rowCount);
		return "familyread";
	}	
	
	@Override
	public String readOne() throws Exception {
		family = (Family) baseService.get(Family.class, Long.valueOf(id));
		if(StringUtils.isNotBlank(this.suc)){
			log.info("#family.getPersons().size()#"+family.getPersons().size());
			return this.suc;
		}	
		return super.readOne();
	}
	@Override
	public String createProcess() throws Exception {		
		JSONObject obj = JSONObject.fromObject(this.jsondata);
		String areaid = obj.getString("areaid");		
		String householderid = obj.optString("householderid");			
		obj.remove("areaid");
		obj.remove("area");
		obj.remove("householderid");
		obj.remove("householdernameR");		
		family = (Family) JSONObject.toBean( obj, Family.class );
		if(!StringUtils.isEmpty(areaid)){
			Area area = new Area();
			area.setId(Long.valueOf(areaid));
			family.setArea(area);
		}	
		if(!StringUtils.isEmpty(householderid)){
			Person person =  (Person) baseService.get(Person.class, Long.valueOf(householderid));	
			family.setHouseholder(person);
		}			
		WebUtil.init4create(family);
//		family.setStatus(status);
		baseService.create(family);		
		return ajaxResult;
	}
	@Override
	public String updateProcess() throws Exception {
		JSONObject obj = JSONObject.fromObject(this.jsondata);
		String areaid = obj.getString("areaid");		
		String householderid = obj.optString("householderid");			
		obj.remove("areaid");
		obj.remove("area");
		obj.remove("householderid");
		obj.remove("householdernameR");			
		family = (Family) JSONObject.toBean( obj, Family.class );
		Family objDb =(Family) baseService.get(Family.class, family.getId());		
		BeanUtil.copyProperties(family,objDb);
		if(!StringUtils.isEmpty(areaid)){
			Area area = new Area();
			area.setId(Long.valueOf(areaid));
			objDb.setArea(area);
		}	
		if(!StringUtils.isEmpty(householderid)){
			Person person =  (Person) baseService.get(Person.class, Long.valueOf(householderid));	
			objDb.setHouseholder(person);
		}	
		WebUtil.init4modify(objDb);	
		baseService.merge(objDb);
		return ajaxResult;
	}	
	
	
	@Override
	public String deleteProcess() throws Exception {
		deleteProcess(Family.class);
		ServletActionContext.getResponse().setContentType("text/plain charset=utf-8");
		ServletActionContext.getResponse().getWriter().write("{success:true}");	
		return super.deleteProcess();
	}	

}